#ifndef __YMODEM_H__
#define __YMODEM_H__
#include "main.h"

/* command of ymodem */
#define SOH                     (0x01)  /* start of 128-byte data packet */
#define STX                     (0x02)  /* start of 1024-byte data packet */
#define EOT                     (0x04)  /* end of transmission */
#define ACK                     (0x06)  /* acknowledge */
#define NAK                     (0x15)  /* negative acknowledge */
#define CA                      (0x18)  /* two of these in succession aborts transfer */
#define CRC16                   (0x43)  /* 'C' == 0x43, request 16-bit CRC*/
#define ABORT1                  (0x41)
#define ABORT2                  (0x61)

/* packet header and tailer size */
#define PACKET_HEADER           (3)
#define PACKET_TRAILER          (2)
/* data size */
#define DATA_SIZE_SOH           (128)
#define DATA_SIZE_STX           (1024)
/* packet size */
#define PACKET_SIZE_SOH         (PACKET_HEADER + DATA_SIZE_SOH + PACKET_TRAILER)
#define PACKET_SIZE_STX         (PACKET_HEADER + DATA_SIZE_STX + PACKET_TRAILER)
/* file name max length */
#define FILE_NAME_LENGTH        (32)
/* file size max length */
#define FILE_SIZE_LENGTH        (16)
/* transmission max err cnt */
#define MAX_ERROR_COUNT         (5)
#define IS_AF(c)                ((c >= 'A') && (c <= 'F'))
#define IS_af(c)                ((c >= 'a') && (c <= 'f'))
#define IS_09(c)                ((c >= '0') && (c <= '9'))
#define ISVALIDHEX(c)           (IS_AF(c) || IS_af(c) || IS_09(c))
#define ISVALIDDEC(c)           IS_09(c)
#define CONVERTDEC(c)           (c - '0')
#define CONVERTHEX_alpha(c)     (IS_AF(c) ? (c - 'A'+10) : (c - 'a'+10))
#define CONVERTHEX(c)           (IS_09(c) ? (c - '0') : CONVERTHEX_alpha(c))

int32_t ymodem_receive(uint8_t *buf);

#endif /* __YMODEM_H__ */